package com.medic.service.impl;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.medic.entity.Assitant;
import com.medic.entity.Doctor;
import com.medic.entity.Group;
import com.medic.entity.GroupUser;
import com.medic.entity.Pharmacist;
import com.medic.entity.User;
import com.medic.entity.VerifyRole;
import com.medic.mapper.interf.GroupMapper;
import com.medic.service.interf.GroupServiceInterface;
import com.medic.util.CacheUtil;



@Service("groupService") 
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) // 注明需要回滚

public class  GroupServiceImpl  implements GroupServiceInterface {
	
	
	
	@Resource
	private GroupMapper groupMapper;
	

	
	@Override
	@Deprecated
	public boolean delete(Group object) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}
	@Deprecated
	@Override
	public boolean save(Group object) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}
	
	

	

	@Override
	@Deprecated
	public boolean update(Group object) throws Exception {
		return false;
	}
	
	
	@Override
	public void assitantBatchJoinGroup(int[] list, int groupid)
			throws Exception {
			groupMapper.insertBatchGroup(list, groupid,"assitant",System.currentTimeMillis(),System.currentTimeMillis());
	}
	@Override
	public void createGroup(Group g) throws Exception {
		groupMapper.save(g);
	}
	

	@Override
	public void assitantJoinGroup(int groupid, int assitantid) throws Exception {
			groupMapper.insertGroup(assitantid, groupid, "assitant",System.currentTimeMillis());
	}


	@Override
	public void assitantExitGroup(int group ,int[] list) throws Exception {
			groupMapper.deleteGrouper(list,group, "assitant");
		
	}
	@Override
	public void dismissGroup(int groupid) throws Exception {
		
		
		groupMapper.deleteGroup(groupid);  
		//删除各个中间表里的数据              
	}

	@Override
	public void doctorBatchJoinGroup(int[] list, int groupid) throws Exception {
			groupMapper.insertBatchGroup(list, groupid , "doctor",System.currentTimeMillis(),System.currentTimeMillis());
	}

	@Override
	public void doctorExitGroup(int group ,int[] list) throws Exception {
			groupMapper.deleteGrouper(list, group,"doctor");
	}

	@Override
	public void doctorJoinGroup(int groupid, int doctorid) throws Exception {
			groupMapper.insertGroup(doctorid, groupid, "doctor",System.currentTimeMillis());
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) 
	public List<Assitant> getAssitantByGroup(int groupid) throws Exception {
		return groupMapper.selectAssitantByGroup(groupid);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) 
	public List<Doctor> getDoctorByGroup(int groupid) throws Exception {
		return groupMapper.selectDoctorByGroup(groupid);
	} 
	
	
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) 
	public List<Pharmacist> getPharmacistByGroup(int groupid) throws Exception {
		 return groupMapper.selectPharmacistByGroup(groupid);
	}

	
	@Override	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)  
	public Page<User> getUserByGroup(int groupid,int pageno,String where) throws Exception {
		PageHelper.startPage(pageno,CacheUtil.getRole_pagesize());
		Page<User> page = (Page<User>)groupMapper.selectUserByGroup(where,groupid);     		
		
		return page;
	}
	
	

	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 注明需要回滚
	public List<Group> getGroup(String where) throws Exception {
		return groupMapper.selectGroup(where);    			
	}

	/**
	 * 分页拿组
	 * @param pageno
	 * @return
	 * @throws Exception
	 */
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 注明需要回滚
	@Override
	public Page<Group> getGroup(int pageno) throws Exception {  
		PageHelper.startPage(pageno,6);
		PageHelper. orderBy("id asc");
		
		Page<Group> page = (Page<Group>)groupMapper.selectGroup(null);    			
		return page;
	}
	
	
	
	
	@Override
	public void pharmacisBatchJoinGroup(int[] list, int groupid)
			throws Exception {
			groupMapper.insertBatchGroup(list, groupid, "pharmacist",System.currentTimeMillis(),System.currentTimeMillis()) ;
	}

	@Override
	public void pharmacistExitGroup(int group ,int[] list) throws Exception {
			groupMapper.deleteGrouper(list,group, "pharmacist");
			
	}

	@Override
	public void pharmacistJoinGroup(int groupid, int pharmacistid)
			throws Exception {
			groupMapper.insertGroup(pharmacistid, groupid, "pharmacist",System.currentTimeMillis());
	}

	@Override
	public void userBatchJoinGroup(int[] list, int groupid) throws Exception {
			groupMapper.insertBatchGroup(list, groupid, "user",System.currentTimeMillis(),System.currentTimeMillis());
	}

	@Override
	public void userExitGroup(int group,int[] list) throws Exception {
			groupMapper.deleteGrouper(list,group, "user");
	}

	@Override
	public void userJoinGroup(int groupid,int userid) throws Exception {
			groupMapper.insertGroup(userid, groupid, "user" ,System.currentTimeMillis());
	}
	
	

	
	@Override
	public void checkUser(int[] list) throws Exception {
			groupMapper.updateGroup(list, 1,"user",System.currentTimeMillis());
	}
	

	@Override
	public void checkDoctor(int[] list) throws Exception {
			groupMapper.updateGroup(list, 1,"doctor",System.currentTimeMillis());	
	}
	@Override
	public void checkPharmacist(int[] list) throws Exception {
			groupMapper.updateGroup(list, 1, "pharmacist" ,System.currentTimeMillis());
	}	
	
	@Override
	public void checkAssitant(int[] list) throws Exception {
		groupMapper.updateGroup(list,1,"assitant",System.currentTimeMillis()); 
		
	}

	
	
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Group> getGroupByUser(int userid) throws Exception {		
		return groupMapper.selectGroupByRole(userid, "user");
	}
	
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Group> getGroupByDoctor(int doctorid) throws Exception {		
		return groupMapper.selectGroupByRole(doctorid, "doctor");
	}
	
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Group> getGroupByAssitant(int assitantid) throws Exception {		
		return groupMapper.selectGroupByRole(assitantid, "assitant");
	}
         
	
	

	
	
	
	
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Page<VerifyRole>  getNopassRole(String role , int pageno) throws Exception {

		PageHelper.startPage(pageno,10);
		
		return (Page<VerifyRole>)groupMapper.selectNopassRole(role);
		
	}
			
	
	
	
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Group> getGroupByPharmacist(int pharmacistid) throws Exception {		
		return groupMapper.selectGroupByRole(pharmacistid, "pharmacist");
	}
	@Override
	public void nopassDoctor(int[] list) throws Exception {

			groupMapper.deleteNopassRole("doctor", list);
	}
	@Override
	public void nopassUser(int[] list) throws Exception {
			groupMapper.deleteNopassRole("user", list);
	}
	@Override
	public void nopassPharmacist(int[] list) throws Exception {
			groupMapper.deleteNopassRole("pharmacist", list);
	}
	@Override
	public void nopassAssitant(int[] list) throws Exception {
			groupMapper.deleteNopassRole("assitant", list);
	}
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Doctor> getDoctorNotinGroup(int groupid, String where) throws Exception {

		return groupMapper.selectDoctorNotinGroup(groupid, where);
		
	}
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Pharmacist> getPharmacistNotinGroup(int groupid, String where) throws Exception {
		return groupMapper.selectPharmacistNotinGroup(groupid, where);
	}
	
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	@Override
	public List<User> getUserNotinGroup(int groupid, String where) throws Exception {
		return groupMapper.selectUserNotinGroup(groupid, where);
	}
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	@Override
	public List<Assitant> getAssitantNotinGroup(int groupid, String where) throws Exception {
		return groupMapper.selectAssitantNotinGroup(groupid, where);
	}
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)	
	@Override
	public int getUserCountByGroup(int group) throws Exception {
			
			return groupMapper.selectCountByGroup( "user" ,group);
	}
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)	
	@Override
	public List<Group> getNotinGroupByRole(int roleid,String role)throws Exception {
			return groupMapper.selectNotinGroupByRole(role, roleid);
	}
	
	@Override
	public GroupUser getGroupUserById(int id) throws Exception {
		groupMapper.selectGroupUserById(id);
		
		return null;
	}

	
	
	/**
	 * 通过组的 id拿到组的详细信息 
	 * @param groupid
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Group getGroupById(int groupid) throws Exception{
		
		return groupMapper.selectGroupById(groupid);
	
	}


	
	

	
}
